package  
{
	import flash.utils.ByteArray;
	import GC;
	import net.flashpunk.Entity;
	import net.flashpunk.graphics.Tilemap;
	import net.flashpunk.masks.Grid;
	
	/**
	 * ...
	 * @author Alex
	 */
	public class Level extends Entity 
	{
		private var _tiles:Tilemap;
		private var _grid:Grid;
		public var LevelData:XML;
		
		public function Level(xml:Class) 
		{
			_tiles = new Tilemap(GC.GFX_TILESET, 640, 480, 32, 32);
			graphic = _tiles;
			layer = 3;
			
			
			_grid = new Grid (640, 480, 32, 32,0,0);			
			mask = _grid;
			
			loadLevel(xml);
			
			type = GC.LEVEL;
			//super();
		}
		
		private function loadLevel(xml:Class):void
		{
			var rawData:ByteArray = new xml;
			var dataString:String = rawData.readUTFBytes(rawData.length);
			LevelData = new XML(dataString);
			
			var dataList:XMLList;
			var dataElement:XML;
			
			dataList = LevelData.terrainTileLayer.tile;
			for each (dataElement in dataList)
			{
				_tiles.setTile(int(dataElement.@x) / 32, int(dataElement.@y / 32), int(dataElement.@tx) / 32);
				_grid.setTile(int(dataElement.@x) / 32, int(dataElement.@y / 32), int(dataElement.@tx) / 32 == GC.T_GROUND);
			}
			
		}
		
	}

}